package com.biwin.module.security.api.properties;

import cn.hutool.core.collection.CollUtil;
import com.biwin.core.main.property.BiWinCheckUrlProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.web.cors.CorsConfiguration;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * BiWin Security Module Property
 *
 * @author biwin
 * @since 0.0.1
 */
@ConditionalOnProperty(value = "biwin.security.enable", havingValue = "false")
@ConfigurationProperties(value = "biwin.security")
public class BiWinSecurityProperties {

    /**
     * 密码使用周期
     * <p>
     * 默认为0,永不过期;如果设置大于0,则以天为单位
     */
    private int passwordUsefulLife = 0;

    /**
     * 密码使用周期的时间单位
     * 配置上 passwordUsefulLife 一起使用.
     * <p>
     * 可选值: s:SECONDS(秒),m:MINUTES(分钟),h:HOURS(小时),d:DAY(天)
     */
    private TimeUnit lifeTimeUnit = TimeUnit.DAYS;

    /**
     * 密码试错最大次数
     * <p>
     * 默认为3,密码试错超过该次数将直接被锁定;如果为0或负数,则表示无限制
     */
    private int passwordMaxFailNumber = 3;

    /**
     * 首次登陆系统是否强制重置密码
     * <p>
     * 默认 false, 不强制修改密码
     */
    private boolean resetPasswordFirstLogin = false;

    @NestedConfigurationProperty
    private BiWinCaptchaProperties captcha = new BiWinCaptchaProperties();

    @NestedConfigurationProperty
    private BiWinCheckUrlProperties ignore = new BiWinCheckUrlProperties();

    @NestedConfigurationProperty
    private BiWinCheckUrlProperties verify = new BiWinCheckUrlProperties();

    /**
     * 允许跨域访问的站点地址
     * 默认包含 "*",表示允许所有站点访问, 一旦配置数据,将仅配置的域名可访问
     */
    private Set<String> corsHosts = CollUtil.newHashSet(CorsConfiguration.ALL);

    /**
     * 允许跨域访问的请求方法
     * 默认包含 "*",表示允许所有请求方法, 一旦配置数据,将仅配置的方法可访问
     */
    private Set<String> corsMethods = CollUtil.newHashSet(CorsConfiguration.ALL);

    /**
     * 允许跨域访问的请求头类型
     * 默认包含 "*",表示允许所有请求头类型, 一旦配置数据,将仅配置的请求头可访问
     */
    private Set<String> corsHeaders = CollUtil.newHashSet(CorsConfiguration.ALL);

    public int getPasswordUsefulLife() {
        return passwordUsefulLife;
    }

    public void setPasswordUsefulLife(int passwordUsefulLife) {
        this.passwordUsefulLife = passwordUsefulLife;
    }

    public TimeUnit getLifeTimeUnit() {
        return lifeTimeUnit;
    }

    public void setLifeTimeUnit(TimeUnit lifeTimeUnit) {
        this.lifeTimeUnit = lifeTimeUnit;
    }

    public int getPasswordMaxFailNumber() {
        return passwordMaxFailNumber;
    }

    public void setPasswordMaxFailNumber(int passwordMaxFailNumber) {
        this.passwordMaxFailNumber = passwordMaxFailNumber;
    }

    public BiWinCaptchaProperties getCaptcha() {
        return captcha;
    }

    public void setCaptcha(BiWinCaptchaProperties captcha) {
        this.captcha = captcha;
    }

    public boolean getResetPasswordFirstLogin() {
        return resetPasswordFirstLogin;
    }

    public void setResetPasswordFirstLogin(boolean resetPasswordFirstLogin) {
        this.resetPasswordFirstLogin = resetPasswordFirstLogin;
    }

    public BiWinCheckUrlProperties getIgnore() {
        return ignore;
    }

    public void setIgnore(BiWinCheckUrlProperties ignore) {
        this.ignore = ignore;
    }

    public BiWinCheckUrlProperties getVerify() {
        return verify;
    }

    public void setVerify(BiWinCheckUrlProperties verify) {
        this.verify = verify;
    }

    public Set<String> getCorsHosts() {
        return corsHosts;
    }

    public void setCorsHosts(Set<String> corsHosts) {
        this.corsHosts.addAll(corsHosts);
    }

    public Set<String> getCorsMethods() {
        return corsMethods;
    }

    public void setCorsMethods(Set<String> corsMethods) {
        this.corsMethods.addAll(corsMethods);
    }

    public Set<String> getCorsHeaders() {
        return corsHeaders;
    }

    public void setCorsHeaders(Set<String> corsHeaders) {
        this.corsHeaders.addAll(corsHeaders);
    }
}
